using LiJ.Common.Encrypt;
using LiJ.Common.Extention;
using System;
using System.Text;

namespace LiJ.Common.Authrization
{
	/// <summary>
	///   应用的授权认证信息
	/// </summary>
	public class AppAuthorizeInfo
	{
		/// <summary>
		///   应用版本
		/// </summary>
		public string AppVersion
		{
			get;
			set;
		}

		/// <summary>
		///   应用来源
		/// </summary>
		public string AppSource
		{
			get;
			set;
		}

		/// <summary>
		/// 应用客户端类型
		/// iOS, Android,PC等用户自定义,可以参考 AppClientType
		/// </summary>
		public string AppClient
		{
			get;
			set;
		}

		/// <summary>
		/// 设备ID
		/// </summary>
		public string DeviceId
		{
			get;
			set;
		}

		/// <summary>
		///  Token 
		/// </summary>
		public string Token
		{
			get;
			set;
		}

		/// <summary>
		/// 时间
		/// </summary>
		public long TimeSpan
		{
			get;
			set;
		}

		/// <summary>
		///  sign标识
		/// </summary>
		public string Sign
		{
			get;
			set;
		}

		/// <summary>
		/// 浏览器类型   可选
		/// </summary>
		public string WebBrowser
		{
			get;
			set;
		}

		/// <summary>
		/// IP地址 可选 手机App端由接收方赋值
		/// </summary>
		public string IpAddress
		{
			get;
			set;
		}

		/// <summary>
		///  租户ID  
		/// </summary>
		public long TenantId
		{
			get;
			set;
		}

		/// <summary>
		///  推广码
		/// </summary>
		public string ProCode
		{
			get;
			set;
		}

		/// <summary>
		///   从头字符串中初始化签名相关属性信息
		/// </summary>
		/// <param name="signData"></param>
		/// <param name="separator">A=a  B=b 之间分隔符</param>
		public virtual void FromSignData(string signData, char separator = ';')
		{
			if (!string.IsNullOrEmpty(signData))
			{
				string[] array = signData.Split(separator);
				foreach (string text in array)
				{
					if (!string.IsNullOrEmpty(text))
					{
						string[] array2 = text.Split(new char[1]
						{
							'='
						}, 2);
					    if (array2.Length <= 1) continue;
					    var text2 = array2[1].UrlDecode();
					    switch (array2[0])
					    {
					        case "av":
					            this.AppVersion = text2;
					            break;
					        case "as":
					            this.AppSource = text2;
					            break;
					        case "ac":
					            this.AppClient = text2;
					            break;
					        case "did":
					            this.DeviceId = text2;
					            break;
					        case "ip":
					            this.IpAddress = text2;
					            break;
					        case "tid":
					            this.TenantId = text2.ToInt64(0);
					            break;
					        case "pc":
					            this.ProCode = text2;
					            break;
					        case "ts":
					            this.TimeSpan = text2.ToInt64(0);
					            break;
					        case "tn":
					            this.Token = text2;
					            break;
					        case "sign":
					            this.Sign = text2;
					            break;
					        case "wb":
					            this.WebBrowser = text2;
					            break;
					    }
					}
				}
			}
		}

		/// <summary>
		/// 生成签名后的字符串
		/// </summary>
		/// <returns></returns>
		public string ToSignData(string secretKey, char separator = ';')
		{
			this.TimeSpan = DateTime.Now.ToUtcSeconds();
			StringBuilder signContent = this.GetSignContent(separator);
			this.Sign = HmacSha1.EncryptBase64(signContent.ToString(), secretKey, null);
			AppAuthorizeInfo.AddSignDataValue("sign", this.Sign, separator, signContent);
			return signContent.ToString();
		}

		/// <summary>
		/// 复制新的授权信息实体
		/// </summary>
		/// <returns></returns>
		public AppAuthorizeInfo Copy()
		{
			return new AppAuthorizeInfo
			{
				AppClient = this.AppClient,
				AppSource = this.AppSource,
				AppVersion = this.AppVersion,
				DeviceId = this.DeviceId,
				IpAddress = this.IpAddress,
				TenantId = this.TenantId,
				Sign = this.Sign,
				TimeSpan = this.TimeSpan,
				Token = this.Token,
				WebBrowser = this.WebBrowser,
				ProCode = this.ProCode
			};
		}

		/// <summary>
		///   检验是否合法
		/// </summary>
		/// <returns></returns>
		public bool CheckSign(string secretKey, char separator = ';')
		{
			string b = HmacSha1.EncryptBase64(this.GetSignContent(separator).ToString(), secretKey, null);
			return this.Sign == b;
		}

		/// <summary>
		///   获取要加密签名的串
		/// </summary>
		/// <param name="separator"></param>
		/// <returns></returns>
		protected internal virtual StringBuilder GetSignContent(char separator)
		{
			StringBuilder stringBuilder = new StringBuilder();
			AppAuthorizeInfo.AddSignDataValue("ac", this.AppClient, separator, stringBuilder);
			AppAuthorizeInfo.AddSignDataValue("as", this.AppSource, separator, stringBuilder);
			AppAuthorizeInfo.AddSignDataValue("av", this.AppVersion, separator, stringBuilder);
			AppAuthorizeInfo.AddSignDataValue("did", this.DeviceId, separator, stringBuilder);
			AppAuthorizeInfo.AddSignDataValue("ip", this.IpAddress, separator, stringBuilder);
			AppAuthorizeInfo.AddSignDataValue("pc", this.ProCode, separator, stringBuilder);
			long num;
			if (this.TenantId > 0)
			{
				num = this.TenantId;
				AppAuthorizeInfo.AddSignDataValue("tid", num.ToString(), separator, stringBuilder);
			}
			num = this.TimeSpan;
			AppAuthorizeInfo.AddSignDataValue("ts", num.ToString(), separator, stringBuilder);
			AppAuthorizeInfo.AddSignDataValue("tn", this.Token, separator, stringBuilder);
			AppAuthorizeInfo.AddSignDataValue("wb", this.WebBrowser, separator, stringBuilder);
			return stringBuilder;
		}

		/// <summary>
		///   追加要加密的串
		/// </summary>
		/// <param name="name"></param>
		/// <param name="value"></param>
		/// <param name="separator"></param>
		/// <param name="strTicketParas"></param>
		protected internal static void AddSignDataValue(string name, string value, char separator, StringBuilder strTicketParas)
		{
			if (!string.IsNullOrEmpty(value))
			{
				if (strTicketParas.Length > 0)
				{
					strTicketParas.Append(separator);
				}
				strTicketParas.Append(name).Append("=").Append(value.UrlEncode());
			}
		}
	}
}
